﻿using System;
using System.Collections;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace iwss.Common
{
    /// <summary>
    /// UsersCollection is object to get all users by selected web object.
    /// </summary> 
    public class UsersCollection
    {
        private ArrayList _usersListCollection;   //All incomming users.
        private SPWeb _selectedWeb;           //Web position.
        private string _groupName;            //Group name.

        private const string _allGroups = "ALL_GROUPS";

        /// <summary>
        /// SPWebLocation property.
        /// </summary>
        public SPWeb SPWebLocation
        {
            get { return _selectedWeb; }
            set { _selectedWeb = value; }
        }

        /// <summary>
        /// SPGroupName property.
        /// </summary>
        public string SPGroupName
        {
            get { return _groupName; }
            set { _groupName = value; }
        }

        /// <summary>
        /// To run the example you need to initialize the UsersCollection object with two instance website and GroupName objects.
        /// </summary>
        /// <param name="webSite">SPWeb object</param>
        /// <param name="groupName">string group name</param>
        public UsersCollection(SPWeb webSite, string groupName)
        {
            //Check if the SPWeb object is not null.
            if (webSite != null)
            {
                if (groupName == null || groupName.Length == 0)
                {
                    this.SPGroupName = _allGroups;
                }
                _usersListCollection = new ArrayList();

            }
            else
            {
                Console.WriteLine("ERROR WEB LOCATION: Your web location have a null value.");
            }
        }

        /// <summary>
        /// This public method get all users from SharePoint web location and get user related information.
        /// </summary>
        public ArrayList GetUsersByGroup()
        {
            if (this.SPGroupName == _allGroups)
            {
                foreach (SPGroup singleGroup in this.SPWebLocation.Groups)
                {
                    UsersList(singleGroup);
                }
            }
            else
            {
                SPGroup singleGroup = this.SearchGroup(this._groupName);
                if (singleGroup != null)
                {
                    this.UsersList(singleGroup);
                }
                else
                {
                    Console.WriteLine("ERROR WEB GROUP: " + this._groupName + " GROUP NOT EXIST.");
                }
            }
            return _usersListCollection;
        }

        /// <summary>
        /// This private method get users by selected SPGroup object.
        /// </summary>
        /// <param name="group">SPGroup object</param>
        private void UsersList(SPGroup group)
        {
            foreach (SPUser singleUser in group.Users)
            {
                foreach (SPRole singleRole in singleUser.Roles)
                {
                    _usersListCollection.Add(new UserListCollection(
                        singleUser.LoginName, singleRole.Name, group.ParentWeb.Title));
                }
            }
        }

        /// <summary>
        /// This method search for SPGroup name.
        /// </summary>
        /// <param name="group">string</param>
        /// <returns>SPGroup object</returns>
        private SPGroup SearchGroup(string group)
        {
            SPGroup groupObject = null;
            foreach (SPGroup singleGroup in this.SPWebLocation.Groups)
            {
                if (group == singleGroup.Name)
                {
                    groupObject = singleGroup;
                }
            }
            return groupObject;
        }
    }

    /// <summary>
    /// Generic class UserListCollection
    /// </summary> 
    public class UserListCollection
    {
        private string _userName = "";
        private string _userRole = "";
        private string _userWeb = "";

        /// <summary>
        /// Generic username.
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }


        /// <summary>
        /// Generic user role.
        /// </summary>
        public string UserRole
        {
            get { return _userRole; }
            set { _userRole = value; }
        }

        /// <summary>
        /// Generic SharePoint web name.
        /// </summary>
        public string WebName
        {
            get { return _userWeb; }
            set { _userWeb = value; }
        }

        /// <summary>
        /// This constructor insert three values.
        /// </summary>
        /// <param name="userName">Name of NT user account.</param>
        /// <param name="userGroup">SharePoint web group name.</param>
        /// <param name="userWeb">SPSite web name.</param>
        public UserListCollection(string userName, string userGroup, string userWeb)
        {
            this.UserName = userName;
            this.UserRole = userGroup;
            this.WebName = userWeb;
        }
    }

 
}
